//
//  QSNavigationController.m
//  test
//
//  Created by Song on 2016/11/11.
//  Copyright © 2016年 Song. All rights reserved.
//

#import "QSNavigationController.h"

/** 导航栏背景颜色 */
#define kNavBgColor [UIColor blueColor]
/** ButtonItem的字体 */
#define kNavBtnItemFont [UIFont systemFontOfSize:17]
/** ButtonItem的颜色 */
#define kNavBtnItemColor [UIColor whiteColor]
/** ButtonItem禁用状态的背景颜色 */
#define kNavBtnItemDisableColor [UIColor lightGrayColor]
/** 返回按钮的图片  如果返回按钮即有图片和文字，自定义图片的大小最好是13 * 21，不带文字就没有特殊规定 */
#define kNavReturnBtnImgName @"btn_back"
/** title的字体 */
#define kNavTitleFont [UIFont systemFontOfSize:20]
/** title的字体颜色 */
#define kNavTitleColor [UIColor whiteColor]

@interface QSNavigationController ()

@end

@implementation QSNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UINavigationBar *navBar = self.navigationBar;
    //去除半透明
    navBar.translucent = NO;
    
    // 设置导航栏title的字体大小和颜色
    [navBar setTitleTextAttributes:@{NSFontAttributeName:kNavTitleFont, NSForegroundColorAttributeName:kNavTitleColor}];
    
    // 返回按钮的图片
    [navBar setBackIndicatorImage:[UIImage imageNamed:kNavReturnBtnImgName]];
    [navBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:kNavReturnBtnImgName]];
    
    // 设置导航栏背景的颜色
    [navBar setBarTintColor:kNavBgColor];
    // 按钮的颜色
    [navBar setTintColor:kNavBtnItemColor];
    
    // 去掉底部的黑线
    [navBar setShadowImage:[UIImage new]];
    
    // 设置item
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    // UIControlStateNormal
    NSMutableDictionary *itemAttrs = [NSMutableDictionary dictionary];
    itemAttrs[NSFontAttributeName] = kNavBtnItemFont;
    [item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
    
    // UIControlStateDisabled
    NSMutableDictionary *itemDisabledAttrs = [NSMutableDictionary dictionary];
    itemDisabledAttrs[NSForegroundColorAttributeName] = kNavBtnItemDisableColor;
    [item setTitleTextAttributes:itemDisabledAttrs forState:UIControlStateDisabled];
}

#pragma mark - private
/**
 *  设置背景图片
 */
- (void)mg_setNavBarBackgroundImage:(UIImage *)bgImage
{
    [self.navigationBar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
}

#pragma mark 重写方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器
        UIViewController *vc = self.viewControllers.lastObject;
#warning - 如果不需要title，可直接设为@""
        vc.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:viewController.title style:UIBarButtonItemStylePlain target:nil action:nil];
        
        // 隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

/**
 *  如果需要动态修改状态栏，最好加上这个方法，否则UIViewController里的preferredStatusBarStyle不会被调用
 */
- (UIViewController *)childViewControllerForStatusBarStyle{
    
    return self.topViewController;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait; // 表示支持竖直方向
}

- (BOOL)shouldAutorotate
{
    return YES;
}
@end
